package com.youxiong.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youxiong.Person;
import com.youxiong.Util.RedisUtil;
import com.youxiong.Util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/test")
public class TestController {
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value = "/redisSelectOne")
    public String redisSelectOne(){
        RedisTemplate template = redisUtil.getSelect_0();
        ValueOperations ops = template.opsForValue();
        Person person = new Person();
        person.setId(2L);
        person.setPassword("123456");
        person.setName("admin");
        Object json = JSONObject.toJSON(person);
        ops.set("person" + System.currentTimeMillis(),json.toString());
        return "success";
    }

    @RequestMapping(value = "/redisSelectTwo")
    public String redisSelectTwo(){
        RedisTemplate template = redisUtil.getSelect_1();
        ValueOperations ops = template.opsForValue();
        ops.set("test","1");
        return "success";
    }
    @RequestMapping(value = "/redisGetTestVal")
    public ResultData redisGetTestVal(){
        RedisTemplate template = redisUtil.getSelect_1();
        ValueOperations ops = template.opsForValue();
        String str = (String) ops.get("person1551533851424");
        Person person = JSONObject.parseObject(str, Person.class);
        return new ResultData().success(person);
    }

}
